package com.ruoyi.znjc.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.system.domain.PestDisease2;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.znjc.domain.ZnjcRuleswarnlog;
import com.ruoyi.znjc.service.IZnjcRuleswarnlogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 规则预警日志Controller
 *
 * @author ZNJC
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/znjc/ruleswarnlog")
public class ZnjcRuleswarnlogController extends BaseController {
    @Autowired
    private IZnjcRuleswarnlogService znjcRuleswarnlogService;

    @PostMapping("/process/{timerId}")
    public AjaxResult processTimerId(@PathVariable Long timerId) {
        try {
            // 你可以自己传入 ruleSnapshot 或者空字符串，这里示例用空字符串
            int result = znjcRuleswarnlogService.processAndInsertLogByTimerId(timerId, "");
            return AjaxResult.success("插入日志成功，影响行数：" + result);
        } catch (Exception e) {
            return AjaxResult.error("失败：" + e.getMessage());
        }
    }


    /**
     * 查询规则预警日志列表
     */
    @PreAuthorize("@ss.hasPermi('znjc:ruleswarnlog:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZnjcRuleswarnlog znjcRuleswarnlog) {
        startPage();
        List<ZnjcRuleswarnlog> list = znjcRuleswarnlogService.selectZnjcRuleswarnlogList(znjcRuleswarnlog);

        ObjectMapper objectMapper = new ObjectMapper();

        for (ZnjcRuleswarnlog item : list) {
            String envSnapshot = item.getEnvSnapshot();
            if (envSnapshot != null && envSnapshot.trim().startsWith("[")) {
                try {
                    // 解析JSON数组字符串为字符串数组
                    String[] arr = objectMapper.readValue(envSnapshot, String[].class);
                    // 用换行符拼接
                    String joined = String.join("\n", arr);
                    item.setEnvSnapshot(joined);
                } catch (Exception e) {
                    // 解析失败，保持原始值不变或记录日志
                }
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出规则预警日志列表
     */
    @PreAuthorize("@ss.hasPermi('znjc:ruleswarnlog:export')")
    @Log(title = "规则预警日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZnjcRuleswarnlog znjcRuleswarnlog) {
        List<ZnjcRuleswarnlog> list = znjcRuleswarnlogService.selectZnjcRuleswarnlogList(znjcRuleswarnlog);
        ExcelUtil<ZnjcRuleswarnlog> util = new ExcelUtil<ZnjcRuleswarnlog>(ZnjcRuleswarnlog.class);
        util.exportExcel(response, list, "规则预警日志数据");
    }

    /**
     * 获取规则预警日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('znjc:ruleswarnlog:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId) {
        return success(znjcRuleswarnlogService.selectZnjcRuleswarnlogByLogId(logId));
    }

    /**
     * 新增规则预警日志
     */
    @PreAuthorize("@ss.hasPermi('znjc:ruleswarnlog:add')")
    @Log(title = "规则预警日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnjcRuleswarnlog znjcRuleswarnlog) {
        return toAjax(znjcRuleswarnlogService.insertZnjcRuleswarnlog(znjcRuleswarnlog));
    }

    /**
     * 修改规则预警日志
     */
    @PreAuthorize("@ss.hasPermi('znjc:ruleswarnlog:edit')")
    @Log(title = "规则预警日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnjcRuleswarnlog znjcRuleswarnlog) {
        return toAjax(znjcRuleswarnlogService.updateZnjcRuleswarnlog(znjcRuleswarnlog));
    }

    /**
     * 删除规则预警日志
     */
    @PreAuthorize("@ss.hasPermi('znjc:ruleswarnlog:remove')")
    @Log(title = "规则预警日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds) {
        return toAjax(znjcRuleswarnlogService.deleteZnjcRuleswarnlogByLogIds(logIds));
    }

    /**
    * 查询病虫害及对应作物类别名称列表
     */
    @PreAuthorize("@ss.hasPermi('znjc:disease:list')")
    @GetMapping("/pestDiseaseWithCategory")
    public List<Map<String, Object>> pestDiseaseWithCategoryList(PestDisease2 pestDisease) {
        return znjcRuleswarnlogService.selectPestDiseaseWithCategoryName(pestDisease);
    }




}
